package com.yzf.wisecoder.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.yzf.wisecoder.wisecodercommon.response.Page;
import com.yzf.wisecoder.wisecodercommon.response.Response;
import com.yzf.wisecoder.wisecodercommon.utils.BeanCopyUtils;
import com.yzf.wisecoder.wisecodercommon.utils.SecurityUtils;
import com.yzf.wisecoder.wisecodercommon.utils.ValidatorUtils;
import com.yzf.wisecoder.wisecodercommon.vo.request.RoleReq;
import com.yzf.wisecoder.wisecodercommon.vo.response.RoleResp;
import com.yzf.wisecoder.wisecoderservice.service.ProjectUserService;
import com.yzf.wisecoder.wisecoderservice.service.RoleService;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ProjectUserEntity;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.RoleEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author jianghuiyong
 * @email jianghuiyong@yunzhangfang.com
 * @date 2020-05-06 17:43:12
 */
@RestController
@RequestMapping("sys/role")
@AllArgsConstructor
@Api(value = "角色控制", tags = "角色控制")
public class RoleController {

    private final RoleService roleService;
    private final ProjectUserService projectUserService;


    /**
     * 查询角色分页
     *
     * @param params
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "roleName", name = "角色名称(用户模糊查询)", dataType = "String")
    })
    public Response<Page<RoleResp>> page(@RequestBody Map<String, Object> params) {
        IPage<RoleEntity> page = roleService.queryPage(params);
        Page<RoleResp> roleRespPage = new Page<>();
        roleRespPage.setCurrent(page.getCurrent()).setPageSize(page.getSize())
                .setTotal(page.getTotal()).setTotalPage(page.getPages())
                .setList(BeanCopyUtils.copyList(page.getRecords(), RoleResp.class));
        return Response.buildSuccessResponseWithData(roleRespPage);
    }

    /**
     * 角色列表
     *
     * @param params
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "roleName", name = "角色名称(用户模糊查询)", dataType = "String")
    })
    public Response<List<RoleResp>> list(@RequestBody Map<String, Object> params) {
        List<RoleEntity> list = roleService.queryList(params);
        List<RoleResp> roleResps = BeanCopyUtils.copyList(list, RoleResp.class);
        return Response.buildSuccessResponseWithData(roleResps);
    }

    /**
     * 保存或修改角色信息
     *
     * @param roleReq
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "保存或修改角色")
    public Response saveOrUpdate(@RequestBody RoleReq roleReq) {
        ValidatorUtils.validateEntity(roleReq);
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleReq, roleEntity);
        roleService.saveOrUpdateRole(roleEntity);
        return Response.buildSuccessResponse();
    }

    /**
     * 查询角色信息
     * @param roleId
     * @return
     */
    @GetMapping("info")
    @ApiOperation(value = "查询角色")
    public Response<RoleResp> info(@RequestParam Long roleId){
        RoleEntity roleEntity = roleService.getById(roleId);
        RoleResp roleResp = new RoleResp();
        BeanUtils.copyProperties(roleEntity, roleResp);
        return Response.buildSuccessResponseWithData(roleResp);
    }

    /**
     * 删除角色信息
     *
     * @param roleId
     * @return
     */
    @ApiOperation("删除角色信息")
    @DeleteMapping("remove")
    public Response remove(@RequestParam Long roleId) {
        List<ProjectUserEntity> list = projectUserService.queryByRoleId(roleId, SecurityUtils.getTenantId());
        if (CollectionUtil.isNotEmpty(list)) {
            return Response.buildErrorResponse("角色下有绑定关系, 不能删除");
        }
        roleService.removeById(roleId);
        return Response.buildSuccessResponse();
    }

}
